package com.suse.warmstill.common.exception;


import com.suse.warmstill.common.model.CallResult;
import lombok.extern.slf4j.Slf4j;

import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import javax.validation.ValidationException;
import java.util.Objects;

import static com.suse.warmstill.common.enums.ErrorCode.VALID_ERROR;
/*
 * 全局异常处理
 * @author 黄靖
 * @date 2022/9/29
 *
 * */

@RestControllerAdvice
@Slf4j
public class GloableExceptionHandler {
    /*
     * 处理全局业务异常
     * */
    @ExceptionHandler(value = BusinessException.class)
    public CallResult GlobalBusinessException(BusinessException e) {
        return CallResult.fail(e.getCode(), e.getMessage());
    }

    /*
     * 处理全局 系统异常
     * */
    @ExceptionHandler(value = SystemException.class)
    public CallResult dealSystemException(SystemException exception) {
        return CallResult.fail(exception.getCode(), exception.getMessage());
    }


    /*
     * 处理全局  参数异常
     * */

    /**
     * 处理参数校验异常
     *
     * @param e 异常
     * @return 接口异常信息
     */
    @ExceptionHandler(ValidationException.class)
    public CallResult handleValidationException(ValidationException e) {
        log.error("ValidationException:", e);
        return CallResult.fail(e.getCause().getMessage());
    }


}
